package org.springblade.modules.admin.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.entity.Elevator;
import org.springblade.modules.admin.entity.ElevatorInsuranceOrder;
import org.springblade.modules.admin.entity.EstateElevator;
import org.springblade.modules.admin.entity.InsuranceCompany;
import org.springblade.modules.admin.service.ElevatorInsuranceOrderService;
import org.springblade.modules.admin.service.ElevatorService;
import org.springblade.modules.admin.service.InsuranceCompanyService;
import org.springblade.modules.admin.vo.ElevatorInsuranceOrderVO;
import org.springblade.modules.admin.vo.InsuranceCompanyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @Date 11:20 2021/8/24
 * @Description {
 * 电梯保单接口
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/elevator/insurance/order")
public class ElevatorInsuranceOrderController {

	@Autowired
	private ElevatorInsuranceOrderService elevatorInsuranceOrderService;

	@Autowired
	private ElevatorService elevatorService;

	@GetMapping("/page")
	public R<IPage<ElevatorInsuranceOrderVO>> page(ElevatorInsuranceOrder elevatorInsuranceOrder, Query query) {
		return R.data(elevatorInsuranceOrderService.selectElevatorInsuranceOrderPage(Condition.getPage(query), elevatorInsuranceOrder));
	}

	@PostMapping()
	public R<Boolean> add(@RequestBody ElevatorInsuranceOrder elevatorInsuranceOrder) {
		int count = elevatorInsuranceOrderService.count(Wrappers.<ElevatorInsuranceOrder>lambdaQuery()
			.eq(ElevatorInsuranceOrder::getOrderNum, elevatorInsuranceOrder.getOrderNum()));
		if (count > 0) {
			throw new ServiceException("保单号重复！");
		}
		Elevator elevator = elevatorService.getById(elevatorInsuranceOrder.getElevatorId());
		elevator.setBxIntact(Convert.toBigDecimal(100));
		elevatorService.updateById(elevator);
		return R.data(elevatorInsuranceOrderService.save(elevatorInsuranceOrder));
	}

	@PutMapping("/{id}")
	public R<Boolean> update(@PathVariable Long id, @RequestBody ElevatorInsuranceOrder elevatorInsuranceOrder) {
		elevatorInsuranceOrder.setId(id);
		int count = elevatorInsuranceOrderService.count(Wrappers.<ElevatorInsuranceOrder>lambdaQuery().ne(ElevatorInsuranceOrder::getId, id)
			.eq(ElevatorInsuranceOrder::getOrderNum, elevatorInsuranceOrder.getOrderNum()));
		if (count > 0) {
			throw new ServiceException("保单号重复！");
		}
		Elevator elevator = elevatorService.getById(elevatorInsuranceOrder.getElevatorId());
		elevator.setBxIntact(Convert.toBigDecimal(100));
		elevatorService.updateById(elevator);
		return R.data(elevatorInsuranceOrderService.updateById(elevatorInsuranceOrder));
	}

	@DeleteMapping()
	public R<Boolean> remove(@RequestParam String ids) {
		return R.status(elevatorInsuranceOrderService.removeByIds(Func.toLongList(ids)));
	}

}
